package cn.edu.ldu.views.main;

import cn.edu.ldu.controller.UserController;
import cn.edu.ldu.entity.FriendsEntity;
import cn.edu.ldu.protocol.Protocol;
import cn.edu.ldu.util.Consts;
import cn.edu.ldu.util.L;
import cn.edu.ldu.util.Translate;
import com.alibaba.fastjson.JSON;

import javax.swing.*;
import java.io.IOException;
import java.net.*;

/**
 *
 * @author DYZ
 */
public class CheckFriendsUI extends javax.swing.JPanel {
    FriendsEntity friends = new FriendsEntity();

    /**
     * Creates new form ChecckFriendsUI
     */
    public CheckFriendsUI(int width, int height, FriendsEntity friendsEntity) {
        initComponents(friendsEntity);
        friends = friendsEntity;
        this.setSize(width,height);
        this.setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents(FriendsEntity friendsEntity) {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btnReset = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtUserId = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtUserRename = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtGender = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtStyle = new javax.swing.JTextField();

        setLayout(new java.awt.BorderLayout());

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/BG.jpg"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 622, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 13, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.PAGE_START);

        btnReset.setText("重置信息");
        btnReset.setEnabled(false);
        btnReset.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnResetMouseClicked(evt);
            }
        });

        btnEdit.setText("修改信息");
        btnEdit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                try {
                    jButton1MouseClicked(evt);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(116, 116, 116)
                                .addComponent(btnReset)
                                .addGap(143, 143, 143)
                                .addComponent(btnEdit)
                                .addContainerGap(158, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addContainerGap(38, Short.MAX_VALUE)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(btnReset)
                                        .addComponent(btnEdit))
                                .addGap(35, 35, 35))
        );

        add(jPanel2, java.awt.BorderLayout.PAGE_END);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("用户信息"));

        jLabel2.setText("账号：");

        txtUserId.setText(friendsEntity.getFriendId().toString());
//        txtUserId.setText("0000001");
        txtUserId.setEnabled(false);

        jLabel3.setText("昵称：");

        txtUsername.setText(friendsEntity.getUserAccountEntity().getRealName());
//        txtUsername.setText("00000001");
        txtUsername.setEnabled(false);

        jLabel4.setText("备注：");

        txtUsername.setText(friendsEntity.getFriendRemark());
//        txtUserRename.setText("张三");
        txtUserRename.setEnabled(false);
        txtUserRename.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtUserRenameMouseClicked(evt);
            }
        });

        jLabel5.setText("性别：");

        if(friendsEntity.getUserAccountEntity().getGender()==null){
            txtGender.setText("");
        }else {
            txtGender.setText(friendsEntity.getUserAccountEntity().getGender().toString());
        }
//        txtGender.setText("女");
        txtGender.setEnabled(false);

        jLabel6.setText("个性签名：");

        if(friendsEntity.getUserAccountEntity().getSignature()==null){
            txtStyle.setText("");
        }else {
            txtStyle.setText(friendsEntity.getUserAccountEntity().getSignature());
        }
//        txtStyle.setText("0000");
        txtStyle.setEnabled(false);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(34, 34, 34)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(jPanel3Layout.createSequentialGroup()
                                                .addComponent(jLabel5)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(txtGender))
                                        .addGroup(jPanel3Layout.createSequentialGroup()
                                                .addComponent(jLabel2)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(txtUserId, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(35, 35, 35)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(jPanel3Layout.createSequentialGroup()
                                                .addComponent(jLabel3)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(30, 30, 30)
                                                .addComponent(jLabel4)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(txtUserRename, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(jPanel3Layout.createSequentialGroup()
                                                .addComponent(jLabel6)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(txtStyle)))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2)
                                        .addComponent(txtUserId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel3)
                                        .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel4)
                                        .addComponent(txtUserRename, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel5)
                                        .addComponent(txtGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel6)
                                        .addComponent(txtStyle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(23, Short.MAX_VALUE))
        );

        add(jPanel3, java.awt.BorderLayout.CENTER);
    }// </editor-fold>

    //修改按钮
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) throws IOException {
        // TODO add your handling code here:
        int res = JOptionPane.showConfirmDialog(null, "是否继续操作", "是否继续", JOptionPane.YES_NO_OPTION);
        if (res == JOptionPane.YES_OPTION) {
            //提交修改
            String friendId = txtUserId.getText();
            String friendRemark = txtUserRename.getText();

            InetAddress remoteAddr = null;
            try {
                remoteAddr = InetAddress.getByName(Consts.ipv4);
                DatagramSocket clientSocket = null;
                clientSocket = new DatagramSocket();

                Protocol protocol = new Protocol();
                protocol.setKey("EditRename");
                protocol.setToken(UserController.getToken());

                FriendsEntity friendsEntity = new FriendsEntity();
                friendsEntity.setUserId(UserController.getUserEntity().getId());
                friendsEntity.setFriendId(Long.valueOf(friendId));
                friendsEntity.setFriendRemark(friendRemark);

                protocol.setData(JSON.toJSONString(friendsEntity));
                data = Translate.ObjectToByte(protocol);

                DatagramPacket packet = new DatagramPacket(data,data.length,remoteAddr,Consts.port);
                clientSocket.send(packet);
                L.OK(packet,CheckFriendsUI.class);

                data = new byte[8096];
                packet = new DatagramPacket(data,data.length,remoteAddr,Consts.port);
                clientSocket.receive(packet);

                L.OK(packet,CheckFriendsUI.class);

                Protocol pro = new Protocol();
                pro = (Protocol) Translate.ByteToObject(packet.getData());
                pro.key();

            } catch (UnknownHostException | SocketException e) {
                e.printStackTrace();
            }
        } else {
            return;
        }
        btnReset.setEnabled(false);
        txtUserRename.setEnabled(false);
    }

    //修改备注
    private void txtUserRenameMouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        txtUserRename.setEnabled(true);
        btnReset.setEnabled(true);
    }

    //重置备注
    private void btnResetMouseClicked(java.awt.event.MouseEvent evt){
        txtUserRename.setText(friends.getFriendRemark());
    }



    // Variables declaration - do not modify
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnEdit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField txtGender;
    private javax.swing.JTextField txtStyle;
    private javax.swing.JTextField txtUserId;
    private javax.swing.JTextField txtUserRename;
    private javax.swing.JTextField txtUsername;
    private byte[] data = new byte[8096];
    // End of variables declaration
}
